//
//  BMHttpTool.m
//  xbrc
//
//  Created by 陈宇 on 15/7/16.
//  Copyright (c) 2015年 陈宇. All rights reserved.
//

NSString *const kDefaultMessage = @"数据加载中...";
NSString *const kBMNetworkRequestDomain = @"com.BlueMobi.chrain";
NSString *const kBMNetworkRequestErrorKey = @"kBMNetworkRequestErrorKey";

const NSUInteger count = 1024 * 1024;

@interface BMHttpTool ()

@end

@implementation BMHttpTool

kSingletonM

- (AFHTTPSessionManager *)manager
{
    if (!_manager) {
        _manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:kServerPath]];
        NSMutableSet *contentTypes= [NSMutableSet setWithSet:_manager.responseSerializer.acceptableContentTypes];
        [contentTypes addObject:@"text/html"];
        _manager.responseSerializer.acceptableContentTypes = contentTypes;
        _manager.requestSerializer.timeoutInterval = 5.f;
    }
    return _manager;
}

- (void)getWith:(BMRequest *)request finish:(finishBlock)finish
{
    [[BMHttpTool sharedInstance].manager GET:[kInterfacePath stringByAppendingPathComponent:request.path] parameters:request.params success:^(NSURLSessionDataTask *task, id responseObject) {
        BMResponse *response = [self responseWithJson:responseObject];
        if (finish) {
            finish(response, nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (finish) {
            BMResponse *response = [BMResponse new];
            response.error = error;
            finish(response, error);
        }
    }];
}

- (void)postWith:(BMRequest *)request finish:(finishBlock)finish
{
    [[BMHttpTool sharedInstance].manager POST:[kInterfacePath stringByAppendingPathComponent:request.path] parameters:request.params success:^(NSURLSessionDataTask *task, id responseObject) {
        BMResponse *response = [self responseWithJson:responseObject];
        if (finish) {
            finish(response, nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (finish) {
            BMResponse *response = [BMResponse new];
            response.error = error;
            finish(response, error);
        }
    }];
}

- (void)uploadWith:(BMRequest *)request finish:(finishBlock)finish
{
    [[BMHttpTool sharedInstance].manager POST:[kInterfacePath stringByAppendingPathComponent:request.path] parameters:request.params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [request.uploadData enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSData *obj, BOOL *stop) {
            [formData appendPartWithFileData:obj name:key fileName:@"a.jpg" mimeType:@"image/jpeg"];
        }];
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        BMResponse *response = [self responseWithJson:responseObject];
        finish(response, nil);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        BMResponse *response = [BMResponse new];
        response.error = error;
        finish(response, error);
    }];
}

- (BMResponse *)responseWithJson:(id)responseObj
{
    BMResponse *response = [BMResponse objectWithKeyValues:responseObj];
    
    id result = responseObj[@"data"];
    
    if (result && ![result isKindOfClass:[NSNull class]] && [result isKindOfClass:[NSDictionary class]]) {
        NSDictionary *pageDict = result[@"page"];
        if (pageDict) {
            response.page = [BMPage objectWithKeyValues:pageDict];
        }
        response.rawResult = result;
        response.emptyResult = NO;
    } else {
        response.emptyResult = YES;
    }
    
    return response;
}

@end
